import siteHeader from "./siteHeader.js";
import backTop    from "./backTop.js";
import render     from "./render.js";

function carts(){}

$.extend(carts.prototype , {
    init : function(id,res){
        console.log("i am carts");
        //获取id 和数据
        this.id  = id;
        this.res = res;
        //获取点击加入购物车按钮
        this.btn_warning = $(".btn-warning");
        //获取加减数量按钮
        this.btn_reduce  = $(".btn-reduce");
        this.btn_add     = $(".btn-add");
        //获取商品总数
        this.shops       = $("#numBer");
        //购物车图标数量
        this.cart_num    = $(".cart-num");
        this.sp_num      = $(".sp-num");
        //点击进入购物车
        this.btn_danger  = $(".btn-danger");
        // this.topbar_cart = $(".topbar-cart");
        // this.backCart    = $(".back-cart");
        
        this.sumCartNum();
        this.btn_reduce.on("click",$.proxy(this.reduceShops,this));
        this.btn_add.on("click",$.proxy(this.addShops,this));
        this.btn_warning.on("click",$.proxy(this.addCarts,this));
        this.btn_warning.on("click",$.proxy(this.sumCartNum,this));
       
        siteHeader.init();
        
        this.btn_danger.on("click",$.proxy(this.goBackCart,this));
        // this.topbar_cart.on("click",$.proxy(this.goBackCart,this));
        // this.backCart.on("click",$.proxy(this.goBackCart,this));
        
        backTop.init();
    },
    reduceShops : function(){
        // console.log("reduce");
        var num = this.shops.val();
        num == 1 ? "" : num --;
        this.shops.val(num);
    },
    addShops : function(){
        // console.log("add");
        var num = this.shops.val();
        num ++;
        this.shops.val(num);      
    },
    addCarts : function(){
        //获取到id
        //添加到localStorage里
        //carts不存在 创建结构放入数据
        //carts 存在 id相同直接count++
        //carts存在 id不同
        var num = parseInt(this.shops.val());     
        var s = localStorage.getItem("carts");
        if(s === null){
            var arr = [{
                id : this.id,
                count : num
            }]
            //localstorage只能存字符串
            localStorage.setItem("carts",JSON.stringify(arr));        
        }else{
            var locals = JSON.parse(s);
            var has_same_id = false;
            $.each(locals , $.proxy(function(index,item){
                // console.log(item.id,this.id)     
                item.id === this.id && (item.count += num) && (has_same_id = true);         
            },this))
            !has_same_id && locals.push({
                id : this.id,
                count :num
            })
            localStorage.setItem("carts",JSON.stringify(locals));

        }
        // console.log(localStorage.getItem("carts"));
    },
    sumCartNum : function(){
        var locals = localStorage.getItem("carts");
        var sum = 0;
        locals = locals === null ? "[]" : locals;
        locals = JSON.parse(locals);

        locals.forEach(function(item){
            sum += item.count;
        });
        this.cart_num.html(sum);
        sum !== 0 && this.sp_num.css("display","none");      
    },
    goBackCart : function(){
        location.href = "http://localhost/js/test/konka/html/carts.html";
    }
})

export default new carts();